package com.zzyl.common.ai;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ChatCompletion;
import com.openai.models.ChatCompletionCreateParams;
import com.openai.models.ResponseFormatJsonObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@AllArgsConstructor
public class AIModelInvoker {

    private final AliBaiLianProperties aliBaiLianProperties;

    public String invoke(String prompt) {


        OpenAIClient client = OpenAIOkHttpClient.builder()
                //.apiKey(System.getenv("DASHSCOPE_API_KEY"))//读取环境变量
                .apiKey(aliBaiLianProperties.getApiKey())
                .baseUrl(aliBaiLianProperties.getUrl())
                .build();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addUserMessage(prompt)
                .responseFormat(ResponseFormatJsonObject.builder().build())//指定返回的数据格式为json
                .model(aliBaiLianProperties.getModelName())
                .build();
            ChatCompletion chatCompletion = client.chat().completions().create(params);
            return chatCompletion.choices().get(0).message().content().get();
    }
}